home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / misc / New8n1.lha / 8n1_040.s < prev    next >
Text File  |  1996-12-14  |  59KB  |  1,907 lines

  1. ********************************************************************************
  2. **                                          **
  3. **  Name       : 8n1_040.s                              **
  4. **  Copyright  : © Copyright 96                           **
  5. **  Author     : Iain Barclay                              **
  6. **  Created    : 02 Dec 96                              **
  7. **  Version    : 37.26                                  **
  8. **                                          **
  9. ********************************************************************************
  10.      ;
  11.      ;      SNMA specific options
  12.      ;
  13.      IFD      SNMA
  14.      ;
  15.      CPU      M68040
  16.      ;
  17.      SNMAOPT  Q,A,M,T,E-,P,R,B
  18.      ;
  19.      ELSE
  20.      IFD PHXASS
  21.      machine 68040
  22.      OPT !
  23.      ENDC
  24.      ENDC
  25.      ;
  26.      ;
  27. DEBUG     SET      0
  28. NEWCODE  SET      1
  29. EOFCODE  SET      1
  30. SETDSR     SET      0
  31. NCOMM     SET      0
  32.      ;
  33.      ;
  34.      ;
  35.      SECTION  text,CODE
  36.      ;
  37.      ;
  38.      ;
  39. DEVICES_SERIAL_I_OBSOLETE  EQU        1
  40.      ;
  41.      ;
  42.      ;
  43.      INCLUDE  "exec/lists.i"
  44.      INCLUDE  "exec/memory.i"
  45.      INCLUDE  "exec/resident.i"
  46.      INCLUDE  "exec/devices.i"
  47.      INCLUDE  "exec/execbase.i"
  48.      INCLUDE  "exec/io.i"
  49.      INCLUDE  "exec/ports.i"
  50.      INCLUDE  "exec/errors.i"
  51.      INCLUDE  "exec/initializers.i"
  52.      INCLUDE  "intuition/preferences.i"
  53.      INCLUDE  "devices/timer.i"
  54.      INCLUDE  "devices/serial.i"
  55.      INCLUDE  "hardware/custom.i"
  56.      INCLUDE  "hardware/cia.i"
  57.      INCLUDE  "hardware/intbits.i"
  58.      INCLUDE  "hardware/adkbits.i"
  59.      INCLUDE  "resources/misc.i"
  60.      INCLUDE  "exec/alerts.i"
  61.      INCLUDE  "exec/macros.i"
  62.      INCLUDE  "8n1.device_rev.i"
  63.      ;
  64.      ;      Define hardware references
  65.      ;
  66.      XREF      _custom
  67.      XREF      _intena,_intenar,_intreq,_intreqr
  68.      XREF      _ciab,_ciabpra
  69.      XREF      _serper,_serdat,_serdatr
  70.      XREF      _adkcon,_adkconr
  71.      ;
  72.      ;      Exec Functions
  73.      ;
  74.      XREF      _LVORemDevice,_LVOOpenDevice,_LVOCloseDevice
  75.      XREF      _LVOSupervisor
  76.      XREF      _LVOAllocMem,_LVOFreeMem,_LVOCopyMem
  77.      XREF      _LVOReplyMsg
  78.      XREF      _LVOSendIO,_LVOAbortIO
  79.      XREF      _LVODisable,_LVOEnable
  80.      XREF      _LVOFindName
  81.      XREF      _LVOOpenResource
  82.      XREF      _LVOAddIntServer,_LVORemIntServer
  83.      XREF      _LVOAlert
  84.      XREF      _LVOOpenLibrary,_LVOCloseLibrary
  85.      ;
  86.      ;      Misc Resource Functions
  87.      ;
  88.      XREF      _LVOAllocMiscResource,_LVOFreeMiscResource
  89.      ;
  90.      ;      Intuition Functions
  91.      ;
  92.      XREF      _LVOGetPrefs
  93.      ;
  94.      ;      Mask used to get rid of the printer bits.
  95.      ;
  96. PRTMASK  EQU      (CIAF_PRTRPOUT|CIAF_PRTRBUSY)
  97.      ;
  98.      ;      Autovector offsets
  99.      ;
  100. LVL1VEC  EQU      (1-1)*4+$64
  101. LVL5VEC  EQU      (5-1)*4+$64
  102.      ;
  103.      ;      Device base
  104.      ;
  105.      STRUCTURE Base8n1,LIB_SIZE
  106.      UBYTE      vb_SaveDDRA
  107.      UBYTE      vb_SavePRA
  108.      APTR      vb_MiscBase
  109.      APTR      vb_OldLevel1
  110.      APTR      vb_OldLevel5
  111.      ULONG      vb_SegList
  112.      ULONG      vb_DefBaud
  113.      ULONG      vb_DefRBufLen
  114.      ULONG      vb_CurRBuf
  115.      ULONG      vb_CurRBufLen
  116.      ULONG      vb_CurBaud
  117.      UBYTE      vb_SerFlags
  118.      UBYTE      vb_Initialized
  119.      LABEL      sizeof_Base8n1
  120.      ;
  121.      ;
  122.      ;
  123. DISABLE  MACRO
  124.      IFNC      '\1',''
  125.      move.w   #INTF_\1,_intena
  126.      ELSE
  127.      jsr      _LVODisable(a6)
  128.      ENDC
  129.      ENDM
  130.      ;
  131. ENABLE     MACRO
  132.      IFNC      '\1',''
  133.      move.w   #INTF_SETCLR|INTF_\1,_intena
  134.      ELSE
  135.      jsr      _LVOEnable(a6)
  136.      ENDC
  137.      ENDM
  138.      ;
  139.      ;
  140.      ;
  141. Start:
  142.      moveq      #-1,d0                  ; set return code
  143.      rts                          ; return
  144.      ;
  145.      ;      RamLib looks for this romtag
  146.      ;
  147. ROMTag     DC.W      RTC_MATCHWORD               ; RT_MATCHWORD
  148.      DC.L      ROMTag                  ; RT_MATCHTAG
  149.      DC.L      ENDTag                  ; RT_ENDSKIP
  150.      DC.B      RTF_AUTOINIT                  ; RT_FLAGS
  151.      DC.B      VERSION                  ; RT_VERSION
  152.      DC.B      NT_DEVICE                  ; RT_TYPE
  153.      DC.B      0                      ; RT_PRI
  154.      DC.L      Name                      ; RT_NAME
  155.      DC.L      IdString                  ; RT_IDSTRING
  156.      DC.L      Init                      ; RT_INIT
  157.      ;
  158.      ;      Perform device initialization
  159.      ;
  160.     cnop 0,4    ; align for 020+
  161. InitRoutine:
  162.      exg      d0,a0                   ; swap seglist and base
  163.      move.l   d0,vb_SegList(a0)              ; store seglist in base
  164.      move.l   a6,SysBase                  ; store in global storage
  165.      exg      a0,d0                   ; swap them back
  166.      rts                          ; return
  167.      ;
  168.      ;
  169.      ;
  170.     cnop 0,4 ; align for 020+
  171. dev_Open:   ; (a1 iorequest, a5 will be Base8n1, a6 sysbase )
  172.      move.l   a5,-(sp)                  ; save registers
  173.      movea.l  a6,a5                   ; save base
  174.      movea.l  SysBase(pc),a6              ; get ExecBase
  175.      ;
  176.      tst.l      d0                      ; unit 0 specified?
  177.      bne.b      50$                      ; nope, error (who cares?)
  178.      ;
  179.      tst.w      LIB_OPENCNT(a5)              ; currently open?
  180.      bne.b      10$                      ; yep, go process
  181.      ;
  182.      move.b   IO_SERFLAGS(a1),vb_SerFlags(a5)     ; save flags
  183.      ;
  184.      tst.b      vb_Initialized(a5)              ; already initialized?
  185.      bne.b      40$                      ; yep, skip initialization
  186.      ;
  187.      bsr      initResources               ; go alloc resources
  188.      tst.l      d0                      ; initialized?
  189.      bne.b      50$                      ; nope, error
  190.      ;
  191.      bra.b      40$                      ; go exit
  192.      ;
  193.      cnop 0,4 ; align for 020+
  194. 10$     moveq      #SerErr_DevBusy,d0              ; preset error status
  195.      btst      #SERB_SHARED,vb_SerFlags(a5)          ; opened shared?
  196.      beq.b      50$                      ; nope, error
  197.      btst      #SERB_SHARED,IO_SERFLAGS(a1)          ; requesting shared?
  198.      beq.b      50$                      ; nope, error
  199.      ;
  200.      ;      Initialize I/O request
  201.      ;
  202. 40$     moveq      #8,d0                   ; get char size
  203.      move.b   d0,IO_READLEN(a1)              ; set read length
  204.      move.b   d0,IO_WRITELEN(a1)              ; set write length
  205.      moveq      #1,d0                   ; get stop bits
  206.      move.b   d0,IO_STOPBITS(a1)              ; set stop bits
  207.      move.b   IO_SERFLAGS(a1),d0
  208.      ori.b      #SERF_XDISABLED|SERF_RAD_BOOGIE|SERF_QUEUEDBRK|SERF_7WIRE,d0 ;flags
  209.      andi.b   #~(SERF_PARTY_ODD|SERF_PARTY_ON),d0 ; not used
  210.      move.b   d0,IO_SERFLAGS(a1)
  211.      move.l   vb_CurBaud(a5),IO_BAUD(a1)          ; set baud
  212.      move.l   vb_CurRBufLen(a5),IO_RBUFLEN(a1)    ; set read buffer length
  213.      ;
  214.      addq.w   #1,LIB_OPENCNT(a5)              ; incr open count
  215.      andi.b   #~(1<<LIBB_DELEXP),LIB_FLAGS(a5)    ; clear expunge bit
  216.      moveq      #0,d0                   ; no error
  217.      ;
  218. 50$     move.b   d0,IO_ERROR(a1)              ; store error code
  219.      movea.l  a5,a6                   ; restore base
  220.      move.l   (sp)+,a5                  ; restore registers
  221.      rts                          ; return
  222.      ;
  223.      ;      Attempt to allocate one of the serial resources.
  224.      ;
  225.      cnop 0,4 ; align for 020+
  226. allocResource:
  227.      move.l   a6,-(sp)                  ; save base pointer
  228.      move.l   d0,-(sp)                  ; save unit
  229.      lea.l      Name(pc),a1                  ; get lock name
  230.      move.l   vb_MiscBase(a5),a6              ; get MiscBase
  231.      jsr      _LVOAllocMiscResource(a6)          ; go allocate it
  232.      tst.l      d0                      ; did we get it?
  233.      beq.b      20$                      ; yep, branch
  234.      ;
  235.      ;      It's in use, so we try to locate the device using the string
  236.      ;      returned and attempt to remove it.
  237.      ;
  238.      movea.l  SysBase(pc),a6              ; get ExecBase
  239.      ;
  240.      movea.l  d0,a1                   ; get ptr to serial name
  241.      lea.l      DeviceList(a6),a0              ; get ptr to device list
  242.      jsr      _LVOFindName(a6)              ; go find it
  243.      tst.l      d0                      ; found?
  244.      beq.b      10$                      ; nope, branch
  245.      ;
  246.      movea.l  d0,a1                   ; xfer device ptr
  247.      jsr      _LVORemDevice(a6)              ; remove it
  248.      ;
  249.      ;      We then retry the allocate.
  250.      ;
  251. 10$     move.l   (sp),d0                  ; get unit
  252.      lea.l      Name(pc),a1                  ; get lock name
  253.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  254.      jsr      _LVOAllocMiscResource(a6)          ; go allocate it
  255.      ;
  256. 20$     addq.l   #4,sp                   ; restore stack ptr
  257.      movea.l  (sp)+,a6                  ; restore base ptr
  258.      rts                          ; return
  259.      ;
  260.      ;
  261.      ;
  262.      cnop 0,4 ; align for 020+
  263. initResources:    ; (a1 iorequest, a5 Base8n1, a6 sysbase )
  264.      move.l   a1,-(sp)                  ; save register
  265.      ;
  266.      lea.l      miscresource(pc),a1              ; ptr to resource name
  267.      jsr      _LVOOpenResource(a6)              ; go open it
  268.      move.l   d0,vb_MiscBase(a5)              ; save base
  269.      ;
  270.      moveq      #MR_SERIALPORT,d0              ; set unit number
  271.      bsr.b      allocResource               ; go allocate it
  272.      tst.l      d0                      ; did we get it?
  273.      bne.b      20$                      ; nope, error
  274.      ;
  275.      moveq      #MR_SERIALBITS,d0              ; set unit number
  276.      bsr.b      allocResource               ; go allocate it
  277.      tst.l      d0                      ; did we get it?
  278.      bne.b      10$                      ; nope, error
  279.      ;
  280.      bsr      getPrefs                  ; get default preferences
  281.      tst.l      d0                      ; got 'em?
  282.      beq.b      30$                      ; yep, branch
  283.      ;
  284.      moveq      #MR_SERIALBITS,d0              ; set unit
  285.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  286.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  287.      ;
  288. 10$     moveq      #MR_SERIALPORT,d0              ; set unit
  289.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  290.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  291.      movea.l  SysBase(pc),a6              ; restore ExecBase
  292.      ;
  293. 20$     moveq      #SerErr_DevBusy,d0              ; set error status
  294.      bra      40$                      ; go return
  295.      ;
  296.      cnop 0,4 ; align for 020+
  297. 30$     moveq      #0,d1                   ; clear flags
  298.      moveq      #UNIT_VBLANK,d0              ; set unit
  299.      lea.l      timerReq(pc),a1              ; ptr to timer request
  300.      lea.l      timerdevice(pc),a0              ; ptr to device name
  301.      jsr      _LVOOpenDevice(a6)              ; go open it
  302.      ;
  303.  
  304.      DISABLE                      ; disable interrupts
  305.      ;
  306.      lea.l      _ciab,a1                  ; get ptr to ciab
  307.      move.b   ciaddra(a1),d0
  308.      move.b   d0,vb_SaveDDRA(a5)              ; save DDR value
  309.      andi.b   #PRTMASK,d0                  ; make serial bits input
  310.      move.b   (a1),d1                  ; ciapra(a1)
  311.      ori.b      #CIAF_COMDTR|CIAF_COMRTS,d0          ; make DTR/RTS output
  312.      move.b   d1,vb_SavePRA(a5)              ; save PR value
  313.      andi.b   #CIAF_COMDTR|CIAF_COMRTS|PRTMASK,d0 ; turn on DTR/RTS
  314.      move.b   d0,ciaddra(a1)
  315.      andi.b   #CIAF_COMCTS|CIAF_COMDSR|PRTMASK,d1 ; make CTS/DSR input
  316.      move.b   d1,(a1)                  ; ciapra(a1)
  317.      ;
  318.      moveq      #INTB_PORTS,d0              ; get interrupt number
  319.      lea.l      VBInterrupt(pc),a1              ; get interrupt ptr
  320.      jsr      _LVOAddIntServer(a6)              ; add it to the list
  321.      ;
  322.      bsr      getVBR                  ; get vector base (in A0)
  323.      ;
  324.      move.l   LVL1VEC(a0),vb_OldLevel1(a5)          ; save original vector
  325.      lea.l      level1(pc),a1               ; get new vector ptr
  326.      move.l   a1,LVL1VEC(a0)              ; set new vector
  327.      ;
  328.      move.l   LVL5VEC(a0),vb_OldLevel5(a5)          ; save original vector
  329.      lea.l      level5(pc),a1               ; get new vector ptr
  330.      move.l   a1,LVL5VEC(a0)              ; set new vector
  331.      ;
  332.      lea.l      _custom,a1                  ; get ptr to custom chips
  333.      move.w   #INTF_RBF|INTF_TBE,intreq(a1)       ; clear pending interrupts
  334.      move.w   #INTF_SETCLR|INTF_RBF|INTF_TBE,intena(a1) ; enable RBF & TBE
  335.      ;
  336.      addq.b   #1,vb_Initialized(a5)           ; set flag
  337.      moveq      #0,d0                   ; set good status
  338.      ;
  339.      ENABLE                       ; enable interrupts
  340.      ;
  341. 40$     movea.l  (sp)+,a1                  ; restore register
  342.      rts                          ; return
  343.      ;
  344.      ;      Get system preferences
  345.      ;
  346.      cnop 0,4 ; align for 020+
  347. getPrefs:
  348.      move.l   a1,-(sp)                  ; save registers
  349.      ;
  350.      lea.l      intuitlib(pc),a1              ; ptr to library name
  351.      moveq      #0,d0                   ; can't use OldOpenLibrary as this will not be supported.
  352.      jsr      _LVOOpenLibrary(a6)              ; go open it (any version)
  353.      movea.l  d0,a6                   ; get intuition base
  354.      ;
  355.      move.l   #(pf_SerParShk+3)&$fffffffc,d0      ; size we need (aligned)
  356.      suba.l   d0,sp                   ; reserve space
  357.      ;
  358.      movea.l  sp,a0                   ; set data area ptr
  359.      jsr      _LVOGetPrefs(a6)              ; get preferences
  360.      ;
  361.      movea.l  a6,a1                   ; get intuition base
  362.      movea.l  SysBase(pc),a6              ; restore ExecBase
  363.      jsr      _LVOCloseLibrary(a6)              ; close it
  364.      ;
  365.      moveq      #$0f,d1                  ; set mask
  366.      and.b      pf_SerStopBuf(sp),d1              ; get bufsize index
  367.      addq.l   #8,d1                   ; calc shift value
  368.      moveq      #2,d0                   ; get 1<<1
  369.      lsl.l      d1,d0                   ; get default bufsize
  370.      move.l   d0,vb_DefRBufLen(a5)              ; and store
  371.      ;
  372.      moveq      #0,d1                   ; clear upper half
  373.      move.w   pf_BaudRate(sp),d1              ; get baud rate
  374.      move.w   baudTable(pc,d1.w*2),d1          ; get baud rate
  375.      move.l   d1,vb_DefBaud(a5)              ; and store
  376.      ;
  377.      bsr      internalReset               ; go init baud and buffer
  378.      ;
  379.      lea.l      (pf_SerParShk+3)&$fffffffc(sp),sp   ; restore stack
  380.      movea.l  (sp)+,a1                  ; restore registers
  381.      rts                          ; return ( status in D0 )
  382.      ;
  383.      ;      Preferences baud lookup table
  384.      ;
  385. baudTable:
  386.      dc.w      112,300,1200,2400,4800,9600,19200,31250,38400,57600,62400,64800
  387.      ;
  388.      ;      Set exception vectors
  389.      ;
  390.      cnop 0,4 ; align for 020+
  391. getVBR:
  392.      move.l   a5,-(sp)                  ; save registers
  393.      lea.l      20$(pc),a5                  ; ptr to routine
  394.      jsr      _LVOSupervisor(a6)              ; get into supervisor state
  395. 10$     movea.l  (sp)+,a5                  ; restore register
  396.      rts                          ; return
  397. 20$     movec.l  vbr,a0                  ; get vector base
  398.      rte                          ; return
  399.      ;
  400.      ;
  401.      ;
  402.      cnop 0,4 ; align for 020+
  403. freeResources:
  404.      move.l   a1,-(sp)                  ; save registers
  405.      ;
  406.      DISABLE                      ; disable interrupts
  407.      ;
  408.      bsr.b      getVBR                  ; get vector base (in A0)
  409.      ;
  410.      moveq      #1,d0                   ; set not restored code
  411.      ;
  412.      lea.l      level1(pc),a1               ; get our vector ptr
  413.      cmpa.l   LVL1VEC(a0),a1              ; do they match?
  414.      bne      10$                      ; nope, can't restore
  415.      ;
  416.      lea.l      level5(pc),a1               ; get our vector ptr
  417.      cmpa.l   LVL5VEC(a0),a1              ; do they match?
  418.      bne      10$                      ; nope, can't restore
  419.      ;
  420.      move.l   vb_OldLevel1(a5),LVL1VEC(a0)          ; restore original vector
  421.      move.l   vb_OldLevel5(a5),LVL5VEC(a0)          ; restore original vector
  422.      ;
  423.      moveq      #INTB_PORTS,d0              ; get interrupt number
  424.      lea.l      VBInterrupt(pc),a1              ; get interrupt ptr
  425.      jsr      _LVORemIntServer(a6)              ; remove it from the list
  426.      ;
  427.      lea.l      _custom,a0                  ; get custom base
  428.      move.w   #INTF_RBF|INTF_TBE,d0           ; indicate serial interrupts
  429.      move.w   d0,intena(a0)               ; disable interrupts
  430.      move.w   d0,intreq(a0)               ; clear pending interrupts
  431.      ;
  432.      bsr      freeBuf                  ; free allocated buffers
  433.      ;
  434.      lea.l      _ciab,a0                  ; get pointer to ciab
  435.      move.b   ciaddra(a0),d0              ; get DDR value
  436.      andi.b   #PRTMASK,d0                  ; save printer bits
  437.      ori.b      #~PRTMASK,d0                  ; set serial bits to output
  438.      move.b   d0,ciaddra(a0)              ; store value
  439.      ;
  440.      move.b   (a0),d0                  ; get PR value ( ciapra(a0) )
  441.      andi.b   #PRTMASK,d0                  ; mask out serial bits
  442.      move.b   vb_SavePRA(a5),d1              ; get saved value
  443.      andi.b   #~PRTMASK,d1                  ; mask out printer bits
  444.      or.b      d1,d0                   ; combine the two
  445.      move.b   d0,(a0)                  ; and store ( ciapra(a0) )
  446.      ;
  447.      move.b   ciaddra(a0),d0              ; get DDR value
  448.      andi.b   #PRTMASK,d0                  ; mask out serial bits
  449.      move.b   vb_SaveDDRA(a5),d1              ; get saved value
  450.      andi.b   #~PRTMASK,d1                  ; mask out printer bits
  451.      or.b      d1,d0                   ; combine the two
  452.      move.b   d0,ciaddra(a0)              ; and store
  453.      ;
  454.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  455.      jsr      _LVOCloseDevice(a6)              ; go close it
  456.      ;
  457.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  458.      moveq      #MR_SERIALBITS,d0              ; set unit
  459.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  460.      ;
  461.      moveq      #MR_SERIALPORT,d0              ; set unit
  462.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  463.      movea.l  SysBase(pc),a6              ; restore ExecBase
  464.      ;
  465.      ENABLE                       ; enable interrupts
  466.      ;
  467.      subq.b   #1,vb_Initialized(a5)           ; clear flag
  468.      moveq      #0,d0                   ; free up everything
  469.      ;
  470. 10$     movea.l  (sp)+,a1                  ; restore registers
  471.      rts                          ; return
  472.      ;
  473.      ;      Device Close routine
  474.      ;
  475.      cnop 0,4 ; align for 020+
  476. dev_Close:
  477.      ;
  478.      moveq      #-1,d0                  ; invalidate
  479.      move.l   d0,IO_DEVICE(a1)              ;   device
  480.      ;
  481.      subq.w   #1,LIB_OPENCNT(a6)              ; decr open count
  482.      bne.b      dev_Null                  ; still open? yep, branch
  483.      ;
  484.      move.l   a5,-(sp)                  ; save registers
  485.      movea.l  a6,a5                   ; save base
  486.      movea.l  SysBase(pc),a6              ; get ExecBase
  487.      ;
  488.      bsr      freeResources               ; free allocated resources
  489.      ;
  490.      movea.l  a5,a6                   ; restore base
  491.      movea.l  (sp)+,a5                  ; restore registers
  492.      ;
  493.      tst.l      d0                      ; freed?
  494.      bne.b      dev_Null                  ; nope, can't expunge, exit
  495.      ;
  496.      clr.b      vb_SerFlags(a6)              ; clear flags
  497.      ;
  498.      btst      #LIBB_DELEXP,LIB_FLAGS(a6)          ; delayed expunge set?
  499.      beq.b      dev_Null                  ; nope, go exit
  500.      bra.b      dev_Expunge
  501.      ;
  502.      ;      Device Expunge routine (also fall through from dev_Close)
  503.      ;
  504.      cnop 0,4 ; align for 020+
  505. dev_Expunge:
  506.      ori.b      #1<<LIBB_DELEXP,LIB_FLAGS(a6)       ; Set expunge flag
  507.      tst.w      LIB_OPENCNT(a6)              ; currently open?
  508.      bne.b      dev_Null                  ; yep, so just exit
  509.      ;
  510.      move.l   vb_SegList(a6),d0              ; get seglist ptr
  511.      move.l   d0,-(sp)                  ; save registers (save D0!)
  512.      ;
  513.      movea.l  a6,a1                   ; get base
  514.      ; This is the REMOVE macro
  515.      move.l   (a1)+,a0
  516.      move.l   (a1),a1
  517.      move.l   a0,(a1)
  518.      move.l   a1,LN_PRED(a0)
  519.      ;
  520.      movea.l  a6,a1                   ; get base
  521.      moveq      #0,d0                   ; clear work
  522.      move.w   LIB_NEGSIZE(a6),d0              ; calculate
  523.      suba.w   d0,a1                   ;   memory address
  524.      add.w      LIB_POSSIZE(a6),d0              ;     and size
  525.      move.l   a6,-(sp)                  ; save registers
  526.      movea.l  SysBase(pc),a6              ; get ExecBase
  527.      jsr      _LVOFreeMem(a6)              ; free it
  528.      ;
  529.      move.l   (sp)+,a6                  ; restore registers
  530.      move.l   (sp)+,d0                  ; restore registers
  531.      rts                          ; return (seglist in D0!)
  532.      ;
  533.      ;      Device "ExtFunc" routine
  534.      ;
  535.      cnop 0,4 ; align for 020+
  536. dev_Null:
  537.      moveq      #0,d0                   ; set return code
  538.      rts                          ; return
  539.      ;
  540.      ;
  541.      cnop 0,4 ; align for 020+
  542. cmdTable dc.w      cmd_Invalid-cmdTable              ; CMD_INVALID
  543.      dc.w      cmd_Reset-cmdTable              ; CMD_RESET
  544.      dc.w      cmd_Read-cmdTable              ; CMD_READ
  545.      dc.w      cmd_Write-cmdTable              ; CMD_WRITE
  546.      dc.w      cmd_Invalid-cmdTable              ; CMD_UPDATE
  547.      dc.w      cmd_Clear-cmdTable              ; CMD_CLEAR
  548.      dc.w      cmd_Invalid-cmdTable              ; CMD_STOP
  549.      dc.w      cmd_Invalid-cmdTable              ; CMD_START
  550.      dc.w      cmd_Flush-cmdTable              ; CMD_FLUSH
  551.      dc.w      sdcmd_Query-cmdTable              ; SDCMD_QUERY
  552.      dc.w      sdcmd_Break-cmdTable              ; SDCMD_BREAK
  553. endTable dc.w      sdcmd_SetParams-cmdTable          ; SDCMD_SETPARAMS
  554.      ;
  555.      ;      Device BeginIO routine
  556.      ;
  557.      cnop 0,4 ; align for 020+
  558. dev_BeginIO:
  559.      move.l   a5,-(sp)                  ; save register
  560.      movea.l  a6,a5                   ; save base
  561.      movea.l  SysBase(pc),a6              ; get ExecBase
  562.      ;
  563.      move.b   #NT_MESSAGE,LN_TYPE(a1)          ; set type
  564.      clr.b      IO_ERROR(a1)                  ; clear error
  565.      ;
  566.      andi.b   #~(IOSERF_QUEUED|IOSERF_ACTIVE),IO_FLAGS(a1) ; clear flags
  567.      ;
  568.      move.w   IO_COMMAND(a1),d0              ; get command
  569.      add.w      d0,d0                   ; multiply by 2
  570.      cmpi.w   #endTable-cmdTable,d0           ; in range?
  571.      bhi.b      30$                      ; nope, error
  572.      ;
  573.      move.w   cmdTable(pc,d0.w),d0              ; get routine offset
  574.      jsr      cmdTable(pc,d0.w)              ; go do it
  575.      tst.l      d0                      ; I/O completed?
  576.      bne.b      19$                      ; nope, go return
  577.      ;
  578. 10$
  579.      btst      #IOB_QUICK,IO_FLAGS(a1)          ; need to reply?
  580.      bne.b      20$                      ; nope, branch
  581. 15$     jsr      _LVOReplyMsg(a6)              ; send it back
  582.      ;
  583. 19$     andi.b   #~(1<<IOB_QUICK),IO_FLAGS(a1)       ; clear quick bit
  584. 20$     movea.l  a5,a6                   ; restore base
  585.      movea.l  (sp)+,a5                  ; restore register
  586.      rts                          ; return
  587.      ;
  588. 30$     move.b   #IOERR_NOCMD,IO_ERROR(a1)          ; invalid command
  589.      bra.b      10$                      ; branch
  590.      ;
  591.      ;      Device AbortIO routine
  592.      ;
  593.      cnop 0,4 ; align for 020+
  594. dev_AbortIO:
  595.      move.l   a5,-(sp)                  ; save registers
  596.      movea.l  a6,a5                   ; save base
  597.      movea.l  SysBase(pc),a6              ; get ExecBase
  598.      ;
  599.      DISABLE                      ; disable interrupts
  600.      ;
  601.      move.b   IO_FLAGS(a1),d1              ; get flags
  602.      btst      #IOSERB_QUEUED,d1              ; queued request?
  603.      bne.b      40$                      ; yep, branch
  604.      ;
  605.      btst      #IOSERB_ACTIVE,d1              ; active request?
  606.      bne.b      10$                      ; nope, just exit
  607.      ;
  608.      move.w   IO_COMMAND(a1),d0              ; get command
  609.      subq.w   #CMD_READ,d0                  ; was it a read?
  610.      beq.b      20$                      ; yep, go process
  611.      ;
  612.      subq.w   #CMD_WRITE-CMD_READ,d0          ; was it a write?
  613.      beq.b      30$                      ; yep, go process
  614.      ;
  615.      subq.w   #SDCMD_BREAK-CMD_WRITE,d0          ; was it a break?
  616.      beq.b      30$                      ; yep, go process
  617.      ;
  618.      ;      Fall through or enter from below
  619.      ;
  620. 10$     ENABLE                       ; enable ints and return
  621. 15$     movea.l  a5,a6                   ; restore base
  622.      movea.l  (sp)+,a5                  ; restore registers
  623.      rts                          ; return
  624.      ;
  625.      ;      Abort an active read request
  626.      ;
  627. 20$     clr.l      cr_IOReq                  ; no longer active
  628.      bra.b      50$                      ; go set flags
  629.      ;
  630.      ;      Abort an active write request
  631.      ;
  632.      cnop 0,4 ; align for 020+
  633. 30$     clr.l      cw_Length                  ; no longer active
  634.      clr.l      cw_IOReq                  ; no longer active
  635.      move.l   cw_Buffer(pc),d0              ; get buffer ptr
  636.      sub.l      IO_DATA(a1),d0              ; calc number of bytes xfer'd
  637.      move.l   d0,IO_ACTUAL(a1)              ; store
  638.      ;
  639.      ;      Force a TBE interrupt to get the next write going.
  640.      ;
  641.      move.w   #INTF_SETCLR|INTF_TBE,_intreq       ; make TBE pending
  642.      bra.b      50$                      ; go set flags
  643.      ;
  644.      ;      Remove I/O from queue.
  645.      ;
  646.      cnop 0,4 ; align for 020+
  647. 40$     move.l   a1,-(sp)                  ; save ptr
  648.      ; This is the REMOVE macro
  649.      move.l   (a1)+,a0
  650.      move.l   (a1),a1
  651.      move.l   a0,(a1)
  652.      move.l   a1,LN_PRED(a0)
  653.      ;
  654.      movea.l  (sp)+,a1                  ; restore ptr
  655.      ;
  656.      ;      Set error and return I/O
  657.      ;
  658. 50$     move.b   #IOERR_ABORTED,IO_ERROR(a1)          ; set error code
  659.      move.b   IO_FLAGS(a1),d1              ; get flags
  660.      ori.b      #1<<IOSERB_ABORT,d1              ; set abort flag
  661.      andi.b   #~(IOSERF_QUEUED|IOSERF_ACTIVE),d1  ; clear flags
  662.      move.b   d1,IO_FLAGS(a1)              ; store flags
  663.      btst      #IOB_QUICK,d1               ; need to reply?
  664.      bne.b      10$                      ; nope, branch
  665.      jsr      _LVOReplyMsg(a6)              ; send it back
  666.      bra.b      10$                      ; branch to return
  667.      ;
  668.      ;      Abort all active/queued commands and reset internal state
  669.      ;
  670.      cnop 0,4 ; align for 020+
  671. cmd_Reset:
  672.      move.l   a1,-(sp)                  ; save I/O request
  673.      bsr.b      cmd_Flush                  ; go abort queued requests
  674.      ;
  675.      DISABLE                      ; disable interrupts
  676.      ;
  677.      ;      This must follow the DISABLE
  678.      ;
  679.      exg      a5,a6                   ; exchange base and ExecBase
  680.      ;
  681.     IFNE NEWCODE
  682.      move.l   cr_IOReq(pc),a1              ; active read?
  683.      beq.b      10$                      ; nope, branch
  684.     ELSE
  685.      move.l   cr_IOReq(pc),d0              ; active read?
  686.      beq.b      10$                      ; nope, branch
  687.      movea.l  d0,a1                   ; get I/O request
  688.     ENDC
  689.      bsr      dev_AbortIO                  ; go abort it
  690.      ;
  691.      ;
  692.      ;
  693.     IFNE NEWCODE
  694. 10$     move.l   cw_IOReq(pc),a1              ; active write?
  695.      beq.b      20$                      ; nope, branch
  696.     ELSE
  697. 10$     move.l   cw_IOReq(pc),d0              ; active write?
  698.      beq.b      20$                      ; nope, branch
  699.      movea.l  d0,a1                   ; get I/O request
  700.     ENDC
  701.      bsr      dev_AbortIO                  ; go abort it
  702.      ;
  703. 20$     exg      a5,a6                   ; restore base and ExecBase
  704.      movea.l  (sp)+,a1                  ; restore I/O request
  705.      moveq      #8,d0                   ; get char size
  706.      move.b   d0,IO_READLEN(a1)              ; set read length
  707.      move.b   d0,IO_WRITELEN(a1)              ; set write length
  708.      moveq      #1,d0                   ; get stop bits
  709.      move.b   d0,IO_STOPBITS(a1)              ; set stop bits
  710.      move.l   vb_DefBaud(a5),IO_BAUD(a1)          ; set to default baud
  711.      move.l   vb_DefRBufLen(a5),IO_RBUFLEN(a1)    ; set to default buflen
  712.      bsr      internalReset               ; go set/verify parameters
  713.      move.b   d0,IO_ERROR(a1)              ; set error code
  714.      ENABLE                       ; enable interrupts
  715.      ;
  716.      ;      Set RC and return
  717.      ;
  718.      moveq      #0,d0                   ; I/O complete
  719.      rts                          ; return
  720.      ;
  721.      ;      Abort all "queued" requests, leaving all active alone.
  722.      ;
  723.      cnop 0,4 ; align for 020+
  724. cmd_Flush:
  725.      movem.l  a1/a2,-(sp)                  ; save registers
  726.      DISABLE                      ; disable interrupts
  727.      ;
  728.      ;      Abort all queued read requests.
  729.      ;
  730.      lea.l      readQ(pc),a2                  ; get ptr to read queue
  731.      bsr.b      20$                      ; branch to abort
  732.      ;
  733.      ;      Abort all queued write requests.
  734.      ;
  735.      lea.l      writeQ(pc),a2               ; get ptr to write queue
  736.      bsr.b      20$                      ; branch to abort
  737.      ;
  738.      ;      Enable, restore, and return to caller
  739.      ;
  740.      ENABLE                       ; enable interrupts
  741.      movem.l  (sp)+,a1/a2                  ; restore registers
  742.      ;
  743.      ;      Set RC and return
  744.      ;
  745.      moveq      #0,d0                   ; I/O complete
  746.      ;
  747.      ;      !!!NOTE!!!  In a mad attempt to save 2 bytes, this RTS is used
  748.      ;      by the subroutine below.  Why waste 'em?  B-)
  749.      ;
  750. 10$     rts                          ; return ( used below too!! )
  751.      ;
  752.      ;      Subroutine to remove and reply each I/O request.
  753.      ;
  754.      cnop 0,4 ; align for 020+
  755. 20$     movea.l  a2,a0                   ; get list ptr
  756.      ; This is the REMHEAD macro
  757. 25$     move.l   (a0),a1
  758.      move.l   (a1),d0
  759.      beq.b      25$
  760.      move.l   d0,(a0)
  761.      exg.l      d0,a1
  762.      move.l   a0,LN_PRED(a1)
  763.      ;
  764.      tst.l      d0                      ; end of list?
  765.      beq.b      10$                      ; yep, branch to return
  766.      ;
  767.      movea.l  d0,a1                   ; get I/O request
  768.      andi.b   #~(1<<IOSERB_QUEUED),IO_FLAGS(a1)   ; no longer queued
  769.      moveq      #IOERR_ABORTED,d0              ; indicate aborted
  770.      move.b   d0,IO_ERROR(a1)              ; store status
  771.      ;
  772.      jsr      _LVOReplyMsg(a6)              ; send it back
  773.      bra.b      20$                      ; continue with next
  774.      ;
  775.      ;      Process a CMD_READ request.
  776.      ;
  777.      cnop 0,4 ; align for 020+
  778. cmd_Read:
  779.      ;
  780.      ;      Zero length requests just get returned.
  781.      ;
  782.      clr.l      IO_ACTUAL(a1)               ; clear bytes read
  783.      move.l   IO_LENGTH(a1),d0              ; get length and test
  784.      beq.b      20$                      ; yep, leave
  785.      ;
  786.      ;      This can be used to circumvent a bug in NComm 3.0 which
  787.      ;      references the buffer even when there was nothing read.
  788.      ;
  789.     IFNE     NCOMM
  790.      move.l   IO_DATA(A1),a0              ; get data pointer
  791.      clr.b      (a0)                      ; clear first byte in buffer
  792.     ENDC
  793.      ;
  794.      ;      The disable counter works just like exec's TDNestCnt field.  It's
  795.      ;      initialized to -1.  After incrementing, if it is 0, then we
  796.      ;      can attempt to process this request immediately.  If it's > 0,
  797.      ;      then we're already disabled and we must queue this request.
  798.      ;
  799.      addq.b   #1,disableRead              ; incr disable count
  800.      bgt.b      50$                      ; >0, already disabled
  801.      ;
  802.      ;      If we're already processing an request, this one has to wait
  803.      ;      until that one is done, so go queue it.
  804.      ;
  805.      tst.l      cr_IOReq(pc)                  ; have an active request?
  806.      bne.b      50$                      ; yep, go queue this one
  807.      ;
  808.      ;      If we don't have enough bytes to satisfy this request then go
  809.      ;      queue it.
  810.      ;
  811.      cmp.l      i_InCnt(pc),d0              ; length > current bytes?
  812.      bgt.b      50$                      ; yep, go queue it
  813.      ;
  814.      ;      Setup fields and go copy the data
  815.      ;
  816.      move.l   IO_DATA(a1),cr_OutPtr           ; get/set output ptr
  817.      move.l   IO_LENGTH(a1),cr_Length          ; get/set output count
  818.      bsr      copyData                  ; go copy 'em
  819.      moveq      #0,d0                   ; I/O complete
  820.      ;
  821.      ;      We're done, so back off the disable counter.
  822.      ;
  823. 10$     subq.b   #1,disableRead              ; decr disable count
  824.      ;
  825.      ;      Return to caller
  826.      ;
  827. 20$     rts                          ; return
  828.      ;
  829.      ;      Just set flags and queue.  The read interrupt will handle it.
  830.      ;
  831. 50$     ori.b      #1<<IOSERB_QUEUED,IO_FLAGS(a1)    ; indicate queued
  832.      ;
  833.      ;      Add this request to the end.
  834.      ;
  835.      DISABLE                      ; disable interrupts
  836.      lea.l      readQ(pc),a0                  ; get pointer to read queue
  837.      ; This is the ADDTAIL macro
  838.      addq.l   #LH_TAIL,a0
  839.      move.l   LN_PRED(a0),d0
  840.      move.l   a1,LN_PRED(a0)
  841.      exg      d0,a0
  842.      movem.l  d0/a0,(a1)
  843.      move.l   a1,(a0)
  844.      ;
  845.      ENABLE                       ; enable interrupts
  846.      ;
  847.      ;      Indicate that this request was not handled immediatly.
  848.      ;
  849.      moveq      #1,d0                   ; I/O not complete
  850.      bra.b      10$                      ; branch to return
  851.      ;
  852.      ;      Process a CMD_WRITE request.
  853.      ;
  854.      cnop 0,4 ; align for 020+
  855. cmd_Write:
  856.      clr.l      IO_ACTUAL(a1)               ; clear bytes written
  857.      tst.l      IO_LENGTH(a1)               ; get length and test
  858.      beq.b      wbexit                  ; yep, leave
  859.      ;
  860.      ;      Entry point for Break command and fall through from cmd_Write.
  861.      ;
  862. sdcmd_Break:
  863.      ;
  864.      ;      Just set flags and queue. The TBE interrupt will handle it.
  865.      ;
  866.      ori.b      #1<<IOSERB_QUEUED,IO_FLAGS(a1)     ; indicate queued
  867.      ;
  868.      ;      Protect.
  869.      ;
  870.      DISABLE                      ; disable interrupts
  871.      ;
  872.      ;      Add request to end of queue.
  873.      ;
  874.      lea.l      writeQ(pc),a0               ; get queue list ptr
  875.      ; This is the ADDTAIL macro
  876.      addq.l   #LH_TAIL,a0
  877.      move.l   LN_PRED(a0),d0
  878.      move.l   a1,LN_PRED(a0)
  879.      exg      d0,a0
  880.      movem.l  d0/a0,(a1)
  881.      move.l   a1,(a0)
  882.      ;
  883.      ;      If we have an active request, don't force interrupt.
  884.      ;
  885.      tst.l      cw_IOReq(pc)                  ; have an active request?
  886.      bne.b      10$                      ; yep, branch
  887.      ;
  888.      ;      Force a TBE interrupt to get the writes going.
  889.      ;
  890.      move.w   #INTF_SETCLR|INTF_TBE,_intreq       ; make TBE pending
  891.      ;
  892.      ;      Enable, set RC and return to caller.
  893.      ;
  894. 10$     ENABLE                       ; enable interrupts
  895.      moveq      #1,d0                   ; I/O not complete
  896. wbexit     rts                          ; return
  897.      ;
  898.      ;      Resets serial read buffer
  899.      ;
  900.      ;      Since this routine is called internally, it must NOT reference
  901.      ;      the I/O request.
  902.      ;
  903.      cnop 0,4 ; align for 020+
  904. cmd_Clear:
  905.      DISABLE                      ; disable interrupts
  906.      ;
  907.      ;      Load registers
  908.      ;
  909.      move.l   vb_CurRBuf(a5),d0              ; get internal buffer ptr
  910.      move.l   vb_CurRBufLen(a5),d1              ; and internal buffer len
  911.      lea.l      i_BufPtr(pc),a0              ; get ptr internal control
  912.      ;
  913.      ;      Initialize global buffer variables
  914.      ;
  915.      move.l   d0,(a0)                  ; store buffer ptr
  916.      move.l   d0,4(a0)                  ; set current input ptr
  917.      move.l   d0,8(a0)                  ; set current output ptr
  918.      add.l      d1,d0                   ; add buffer length
  919.      move.l   d0,12(a0)                  ; store ptr to end of buffer
  920.      clr.l      16(a0)                  ; clear byte cnt
  921.      move.l   vb_CurBaud(a5),d0              ; get internal baud
  922.      lsr.l      #4,d0                   ; divide by 16
  923.      sub.l      d0,d1                   ; subtract from length
  924.      move.l   d1,20(a0)                  ; set threshold
  925.      ;
  926.      ;      Enable, set RC and return.
  927.      ;
  928.      ENABLE                       ; enable interrupts
  929.      moveq      #0,d0                   ; I/O complete
  930.      rts                          ; return
  931.      ;
  932.      ;
  933.      ;
  934.      cnop 0,4 ; align for 020+
  935. cmd_Invalid
  936.      move.b   #IOERR_NOCMD,IO_ERROR(a1)          ; set bad status
  937.      moveq      #0,d0                   ; I/O complete
  938.      rts                          ; return
  939.      ;
  940.      ;      Returns number of bytes currently in internal buffer and
  941.      ;      current serial port status.
  942.      ;
  943.      ;      NOTE:  Not completely compatible with standard serial.device
  944.      ;      since it doesn't return the upper byte of IO_STATUS.
  945.      ;
  946.      cnop 0,4 ; align for 020+
  947. sdcmd_Query:
  948.      DISABLE                      ; disable interrupts
  949.      moveq      #0,d0                   ; clear d0
  950.      move.b   _ciabpra,d0                  ; get PR register
  951.      andi.w   #~PRTMASK,d0                  ; zap printer bits
  952.      ;
  953.      ;      To use this Set SETDSR to one.  This was done
  954.      ;      for a user whose DSR pin did not function.
  955.      ;
  956.     IFNE SETDSR
  957.      andi        #~(1<<CIAB_COMDSR),d0              ; set DSR
  958.     ENDC
  959.      ;
  960.      ;
  961.      ;
  962.      move.w   d0,IO_STATUS(a1)              ; store status
  963.      move.l   i_InCnt(pc),IO_ACTUAL(a1)          ; byte left in buffer
  964.      ;
  965.      ;      Enable, set RC and return.
  966.      ;
  967.      ENABLE                       ; enable interrupts
  968.      moveq      #0,d0                   ; I/O complete
  969.      rts                          ; return
  970.      ;
  971.      ;
  972.      ;
  973.      cnop 0,4 ; align for 020+
  974. sdcmd_SetParams:
  975.      ;
  976.      ;      Validate the read, write, and stop bit lengths.
  977.      ;
  978.      moveq      #8,d0                   ; get char length
  979.      cmp.b      IO_READLEN(a1),d0              ; 8 bit chars for read?
  980.      bne.b      40$                      ; nope, branch
  981.      cmp.b      IO_WRITELEN(a1),d0              ; 8 bit chars for write?
  982.      bne.b      40$                      ; nope, branch
  983.      moveq      #1,d0                   ; get stop bits
  984.      cmp.b      IO_STOPBITS(a1),d0              ; 1 stop bit?
  985.      bne.b      40$                      ; nope, branch
  986.      ;
  987.      ;      Get and validate the baud rate.
  988.      ;
  989.      move.l   IO_BAUD(a1),d1              ; get baud from I/O req
  990.      bne.b      20$                      ; specified?
  991.      move.l   vb_CurBaud(a5),d1              ; get current baud from base
  992.      bne.b      20$                      ; specified?
  993.      move.l   vb_DefBaud(a5),d1              ; get default baud from base
  994. 20$     cmpi.l   #110,d1                  ; too low?
  995.      blt.b      40$                      ; error
  996.      cmpi.l   #292000,d1                  ; too high?
  997.      bgt.b      40$                      ; error
  998.      ;
  999.      ;      Get and validate the buffer length.
  1000.      ;
  1001.      move.l   IO_RBUFLEN(a1),d0              ; get buffer length
  1002.      bne.b      30$                      ; specified?
  1003.      move.l   vb_CurRBufLen(a5),d0              ; get current from base
  1004.      bne.b      30$                      ; specified?
  1005.      move.l   vb_DefRBufLen(a5),d0              ; get default from base
  1006.      ;
  1007. 30$     bsr.b      internalReset               ; go init baud and buffer
  1008. 35$     move.b   d0,IO_ERROR(a1)              ; set error code
  1009.      bne.b      39$
  1010.      ;
  1011.      ;      If the 7wire bit is not on, we will only use 3-wire protocol
  1012.      ;
  1013.      moveq      #0,d0                   ; clear flag
  1014.      btst      #SERB_7WIRE,IO_SERFLAGS(a1)          ; use 7wire handshaking?
  1015.      beq.b      36$                      ; nope, branch
  1016.      moveq      #1,d0                   ; set flag
  1017. 36$     move.b   d0,Handshake                  ; store flag
  1018.      ;
  1019.      ;      Set RC and return.
  1020.      ;
  1021. 39$     moveq      #0,d0                   ; I/O complete
  1022.      rts                          ; return
  1023.      ;
  1024.      ;      Invalid parm detected.
  1025.      ;
  1026. 40$     moveq      #SerErr_InvParam,d0              ; set error
  1027.      bra.b      35$                      ; go return
  1028.      ;
  1029.      ;      Reset the buffer and baud rate
  1030.      ;
  1031.      ;      Registers:  D0 = Buffer length
  1032.      ;              D1 = Baud rate
  1033.      ;
  1034.      cnop 0,4 ; align for 020+
  1035. internalReset:
  1036.      ;
  1037.      ;      Disable interrupts.
  1038.      ;
  1039.      DISABLE                      ; disable interrupts
  1040.      ;
  1041.      ;      Save buffer length and go set serper.
  1042.      ;
  1043.      move.l   d0,-(sp)                  ; save D0
  1044.      move.l   d1,d0                   ; get baud rate
  1045.      bsr.b      setPeriod                  ; go set the serper register
  1046.      move.l   (sp)+,d0                  ; restore D0
  1047.      ;
  1048.      ;      Determine if the buffer length is adequate for the selected CPS.
  1049.      ;      If not, use 64K for the length.
  1050.      ;
  1051.      move.l   vb_CurBaud(a5),d1              ; get current baud
  1052.      lsr.l      #$3,d1                  ; divide by 8
  1053.      cmp.l      d1,d0                   ; buflen > CPS
  1054.      bhi.b      10$                      ; yep, branch
  1055.      move.l   #65536,d0                  ; else use 64K
  1056.      ;
  1057. 10$     bsr.b      allocBuf                  ; go allocate a new buffer
  1058.      ;
  1059.      ;      Enable and return to caller.
  1060.      ;
  1061.      ENABLE                       ; enable interrupts
  1062.      rts                          ; return (D0 has status)
  1063.      ;
  1064.      ;      Set serial period register
  1065.      ;
  1066.      cnop 0,4 ; align for 020+
  1067. setPeriod:
  1068.      cmp.l      vb_CurBaud(a5),d0              ; current baud = new baud?
  1069.      beq.b      40$                      ; yep, just exit
  1070.      move.l   d0,vb_CurBaud(a5)              ; save new baud
  1071.      move.l   d0,d1                   ; save again
  1072.      lsl.l      #3,d0                   ; baud *= 8
  1073.      sub.l      d1,d0                   ; baud -= saved baud
  1074.      move.l   #25000000,d1                  ; get NTSC base
  1075.      cmpi.b   #50,PowerSupplyFrequency(a6)          ; PAL machine?
  1076.      bne.b      10$                      ; nope, branch
  1077.      move.l   #24772416,d1                  ; get PAL base
  1078. 10$     cmpi.l   #$FFFF,d0                  ; Divide
  1079.      ble.b      20$                      ;
  1080.      lsr.l      #5,d0                   ;
  1081.      divu.w   d0,d1                   ;
  1082.      andi.l   #$FFFF,d1                  ;
  1083.      lsr.l      #5,d1                   ;
  1084.      bra.b      30$                      ;
  1085.      cnop 0,4 ; align for 020+
  1086. 20$     divu.w   d0,d1                   ;
  1087. 30$     move.w   d1,_serper                  ; set period value
  1088. 40$     rts
  1089.      ;
  1090.      ;      Allocate new internal buffer
  1091.      ;
  1092.      cnop 0,4 ; align for 020+
  1093. allocBuf:
  1094.      cmp.l      vb_CurRBufLen(a5),d0              ; len same as previous?
  1095.      beq.b      10$                      ; yep, so no need to alloc
  1096.      move.l   d0,d1                   ; save length
  1097.      movem.l  d1/a1,-(sp)                  ; save registers
  1098.      moveq      #MEMF_PUBLIC,d1              ; public memory
  1099.      jsr      _LVOAllocMem(a6)              ; go allocate it
  1100.      movem.l  (sp)+,d1/a1                  ; restore registers
  1101.      tst.l      d0                      ; did we get it?
  1102.      beq.b      20$                      ; if zero, error
  1103.      bsr.b      freeBuf                  ; go free previous buffer
  1104.      move.l   d0,vb_CurRBuf(a5)              ; store new ptr
  1105.      move.l   d1,vb_CurRBufLen(a5)              ; and length
  1106.      bsr      cmd_Clear                  ; go setup buffer
  1107. 10$     moveq      #0,d0                   ; success
  1108. 15$     rts                          ; return
  1109. 20$     moveq      #SerErr_BufErr,d0              ; set error status
  1110.      bra.b      15$                      ; return
  1111.      ;
  1112.      ;      Free internal buffer
  1113.      ;
  1114.      cnop 0,4 ; align for 020+
  1115. freeBuf:
  1116.      movem.l  d0-d1/a0-a1,-(sp)              ; save registers
  1117.      move.l   vb_CurRBuf(a5),d0              ; is one there?
  1118.      beq.b      10$                      ; no so branch
  1119.      movea.l  d0,a1                   ; get ptr
  1120.      move.l   vb_CurRBufLen(a5),d0              ; get length
  1121.      clr.l      vb_CurRBuf(a5)              ; clear
  1122.      clr.l      vb_CurRBufLen(a5)              ; clear
  1123.      jsr      _LVOFreeMem(a6)              ; free it
  1124. 10$     movem.l  (sp)+,d0-d1/a0-a1              ; restore registers
  1125.      rts                          ; return
  1126.      ;
  1127.      ;      Checks CTS status and if clear generates a TBE interrupt or
  1128.      ;      requeues the timer request.
  1129.      ;
  1130.      ;      Entered from Exec using the MsgPort callback.
  1131.      ;
  1132.      ;      Input:   a6 = ExecBase
  1133.      ;      Output:  none
  1134.      ;
  1135.      ;      No need to preserve d0/d1/a0/a1
  1136.      ;
  1137.      cnop 0,4 ; align for 020+
  1138. timerRtn:
  1139.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1140.      ; This is the REMOVE macro
  1141.      move.l   (a1)+,a0
  1142.      move.l   (a1),a1
  1143.      move.l   a0,(a1)
  1144.      move.l   a1,LN_PRED(a0)
  1145.      ;
  1146.      ;      If we were breaking, reset adkcon.
  1147.      ;
  1148.      lea      _custom,a0                  ; get ptr to custom regs
  1149.      btst      #ADKB_UARTBRK-8,adkconr(a0)          ; were we breaking?
  1150.      beq.b      10$                      ; nope, skip reset
  1151.      ;
  1152.      move.w   #ADKF_UARTBRK,adkcon(a0)          ; stop breaking
  1153.      ;
  1154.      ;
  1155.      ;
  1156. 10$     tst.b      Handshake(pc)               ; are we handshaking?
  1157.      beq.b      20$                      ; nope, generate interrupt
  1158.      btst      #CIAB_COMCTS,_ciabpra           ; clear to send?
  1159.      beq.b      20$                      ; yep, go start writing
  1160.      ;
  1161.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1162.      move.l   #1000,IOTV_TIME+TV_MICRO(a1)          ; wait for .001 seconds
  1163.      jsr      _LVOSendIO(a6)              ; go queue it
  1164.      bra.b      30$                      ; branch to return
  1165.      ;
  1166.      ;      CTS is clear so generate TBE interrupt to restart writing
  1167.      ;
  1168.      cnop 0,4 ; align for 020+
  1169. 20$     move.w   #INTF_SETCLR|INTF_TBE,intreq(a0)    ; set TBE interrupt
  1170. 30$     rts                          ; return
  1171.      ;
  1172.      ;      Non serial interrupt
  1173.      ;
  1174.      ;  a0 - custom chips base
  1175.      ;  a1 - is_Data
  1176.      cnop 0,4 ; align for 020+
  1177. level1n:
  1178.      movem.l  d0-d1/a0-a1/a5-a6,-(sp)          ; save registers
  1179.      lea      _custom,a0                  ; get ptr to custom regs
  1180.      move.w   intenar(a0),d1              ; get enabled interrupts
  1181.      and.w      intreqr(a0),d1              ; and in requested interrupts
  1182.      movea.l  SysBase(pc),a6              ; get ExecBase
  1183.      ;
  1184.      btst      #INTB_DSKBLK,d1              ; Disk block done?
  1185.      beq.b      10$                      ; nope, branch
  1186.      ;
  1187.      movem.l  IVDSKBLK(a6),a1/a5              ; get data and code ptrs
  1188.      pea.l      20$(pc)                  ; push return address
  1189.      jmp      (a5)                      ; jump to routine
  1190.      ;
  1191. 10$     btst      #INTB_SOFTINT,d1              ; software interrupt?
  1192.      beq.b      20$                      ; nope, branch
  1193.      ;
  1194.      movem.l  IVSOFTINT(a6),a1/a5              ; get data and code ptrs
  1195.      pea.l      20$(pc)                  ; push return address
  1196.      jmp      (a5)                      ; jump to routine
  1197.      ;
  1198. 20$     movem.l  (sp)+,d0-d1/a0-a1/a5-a6          ; restore registers
  1199.      rte                          ; return
  1200.      ;
  1201.      ;      Level 1 interrupt handler
  1202.      ;
  1203.      ;  a1 - is_Data
  1204.      ;
  1205.      cnop 0,4 ; align for 020+
  1206. level1:
  1207.      btst      #INTB_INTEN-8,_intenar          ; interrupts enabled?
  1208.      beq.b      35$                      ; nope, ignore
  1209.      btst      #INTB_SOFTINT,_intreqr+1          ; software interrupt?
  1210.      bne.b      level1n                  ; nope, branch
  1211.      btst      #INTB_TBE,_intreqr+1              ; xmit buffer empty?
  1212.      beq.b      level1n                  ; nope, invoke old handler
  1213.      ;
  1214.      ;      Handle "Transmit Buffer Empty" interrupt (write)
  1215.      ;
  1216.      move.w   #INTF_TBE,_intreq              ; clear interrupt
  1217.      move.l   d0,-(sp)                  ; save D0 (faster than MOVEM)
  1218.      move.l   a0,-(sp)                  ; save A0 (faster than MOVEM)
  1219.      ;
  1220.      ;      If we're not handshaking, bypass it.
  1221.      ;
  1222. 10$     tst.b      Handshake(pc)               ; are we handshaking?
  1223.      beq.b      20$                      ; nope, skip CTS test
  1224.      btst      #CIAB_COMCTS,_ciabpra           ; clear to send?
  1225.      bne.b      40$                      ; nope, branch
  1226.      ;
  1227.      ;      If cw_Length goes negative here, we are either done with a
  1228.      ;      request or we were called as a result of a fake interrupt
  1229.      ;      to force us to get the next request going.
  1230.      ;
  1231. 20$     subq.l   #1,cw_Length                  ; decr write length
  1232.      blt.b      60$                      ; < zero, done, branch
  1233.      ;
  1234.      ;      Currently processing a request.
  1235.      ;
  1236.      movea.l  cw_Buffer(pc),a0              ; get buffer ptr
  1237.      move.w   #256,d0                  ; set stop bit
  1238.      move.b   (a0)+,d0                  ; get next byte
  1239.      move.l   a0,cw_Buffer                  ; store buffer ptr
  1240.      move.w   d0,_serdat                  ; store in serdat reg
  1241.      ;
  1242. 30$     movea.l  (sp)+,a0                  ; restore registers
  1243.      move.l   (sp)+,d0                  ; restore registers
  1244. 35$     rte                          ; return
  1245.      ;
  1246.      ;      Queue a timer request to recheck CTS status
  1247.      ;
  1248. 40$     movem.l  d1/a1/a6,-(sp)              ; save registers
  1249.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1250.      move.l   #1000,IOTV_TIME+TV_MICRO(a1)          ; wait for .001 seconds
  1251.      movea.l  SysBase(pc),a6              ; get ExecBase
  1252.      jsr      _LVOSendIO(a6)              ; queue the request
  1253.      movem.l  (sp)+,d1/a1/a6              ; restore registers
  1254.      bra.b      30$                      ; go return
  1255.      ;
  1256.      ;      There aren't anymore requests, so clear and exit
  1257.      ;
  1258.      cnop 0,4 ; align for 020+
  1259. 50$     clr.l      cw_Length-Start(a6)              ; clear length
  1260.      clr.l      cw_IOReq-Start(a6)              ; clear
  1261.      movem.l  (sp)+,d1/a1/a6              ; restore registers
  1262.      bra.b      30$                      ; go return
  1263.      ;
  1264.      ;      Write request completed
  1265.      ;
  1266.      cnop 0,4 ; align for 020+
  1267. 60$     movem.l  d1/a1/a6,-(sp)              ; save registers
  1268.      ;
  1269.      move.l   cw_IOReq(pc),d0              ; active I/O request?
  1270.      beq.b      70$                      ; nope, branch
  1271.      ;
  1272.      ;      Reply it and setup for next
  1273.      ;
  1274.      movea.l  d0,a1                   ; get I/O request
  1275.      andi.b   #~(1<<IOSERB_ACTIVE),IO_FLAGS(a1)   ; no longer active
  1276.      clr.b      IO_ERROR(a1)                  ; no error
  1277.      movea.l  SysBase(pc),a6              ; get ExecBase
  1278.      jsr      _LVOReplyMsg(a6)              ; return I/O request
  1279.      ;
  1280. 70$     lea.l      Start(pc),a6                  ; get section base
  1281.      ;
  1282.      lea.l      writeQ(pc),a1               ; get ptr to write queue
  1283.      move.l   (a1),a0                  ; get head of list
  1284.      move.l   (a0),d0                  ; get successor
  1285.      beq.b      50$                      ; end of list? yep, branch
  1286.      ;
  1287.      ;      Remove the node from the list
  1288.      ;
  1289.      move.l   d0,(a1)                  ; make new head
  1290.      exg.l      d0,a0                   ; swap nodes
  1291.      move.l   a1,LN_PRED(a0)              ; store predecessor
  1292.      ;
  1293.      move.l   d0,a1                   ; get I/O request
  1294.      ;
  1295.      ;      If it's a BREAK, then branch to process as such.
  1296.      ;
  1297.      cmpi.w   #SDCMD_BREAK,IO_COMMAND(a1)          ; BREAK command?
  1298.      beq.b      100$                      ; yep, branch
  1299.      ;
  1300.      ;      Check for absolute length
  1301.      ;
  1302.      move.l   IO_DATA(a1),a0              ; get data ptr
  1303.      moveq      #-1,d1                  ; get value
  1304.      move.l   IO_LENGTH(a1),d0              ; get length
  1305.      cmp.l      d1,d0                   ; length = -1?
  1306.      bne.b      90$                      ; nope, skip scan
  1307.      ;
  1308.      ;      Scan the data for null to calc the length
  1309.      ;
  1310.      move.l   a0,d1                   ; save data ptr
  1311.     ; Buffer size is multiple of 64bytes so we can safely move a long
  1312. 80$     move.l   (a0),d0                  ; move a long into d0
  1313.      addq.l   #1,a0                   ; update pointer
  1314.      tst.b      d0                      ; does it equal 0? (first byte)
  1315.      beq.b      81$                      ; yep, finish
  1316.      addq.l   #1,a0                   ; update pointer
  1317.      lsr.l      #8,d0                   ; shift too second byte
  1318.      tst.b      d0                      ; does it equal 0? (second byte)
  1319.      beq.b      81$                      ; yep, finish
  1320.      addq.l   #1,a0                   ; update pointer
  1321.      lsr.l      #8,d0                   ; shift too third byte
  1322.      tst.b      d0                      ; does it equal 0? (third byte)
  1323.      beq.b      81$                      ; yep, finish
  1324.      addq.l   #1,a0                   ; update pointer
  1325.      lsr.l      #8,d0                   ; shift too fourth byte
  1326.      tst.b      d0                      ; does it equal 0? (fourth byte)
  1327.      beq.b      81$                      ; yep, finish
  1328.      bra.b      80$                      ; loop
  1329.      cnop 0,4 ; align for 020+
  1330. 81$
  1331.      suba.l   d1,a0                   ; calc # of bytes
  1332.      move.l   a0,d0                   ; xfer
  1333.      movea.l  d1,a0                   ; get data ptr
  1334.      ;
  1335. 90$     move.l   d0,IO_ACTUAL(a1)              ; go ahead and set it
  1336.      move.b   IO_FLAGS(a1),d1
  1337.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1338.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1339.      move.b   d1,IO_FLAGS(a1)
  1340.      move.l   d0,cw_Length-Start(a6)          ; store length
  1341.      move.l   a0,cw_Buffer-Start(a6)          ; store buffer ptr
  1342.      move.l   a1,cw_IOReq-Start(a6)           ; store I/O request ptr
  1343.      ;
  1344.      movem.l  (sp)+,d1/a1/a6              ; restore registers
  1345.      bra      10$                      ; go start request
  1346.      ;
  1347.      ;      Start the BREAK.
  1348.      ;
  1349.      cnop 0,4 ; align for 020+
  1350. 100$     move.b   IO_FLAGS(a1),d1
  1351.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1352.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1353.      move.b   d1,IO_FLAGS(a1)
  1354.      clr.l      cw_Length-Start(a6)              ; clear length
  1355.      move.l   a1,cw_IOReq-Start(a6)           ; store I/O request ptr
  1356.      move.w   #ADKF_SETCLR|ADKF_UARTBRK,_adkcon   ; start break
  1357.      move.l   IO_BRKTIME(a1),d0              ; get break time
  1358.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1359.      move.l   d0,IOTV_TIME+TV_MICRO(a1)          ; set the timeout
  1360.      movea.l  SysBase(pc),a6              ; get ExecBase
  1361.      jsr      _LVOSendIO(a6)              ; queue the request
  1362.      movem.l  (sp)+,d1/a1/a6              ; restore registers
  1363.      bra      30$                      ; go exit
  1364.      ;
  1365.      ;      Non serial interrupt
  1366.      ;
  1367.      ;  a0 - custom chips base
  1368.      ;  a1 - is_Data
  1369.      ;
  1370.      cnop 0,4 ; align for 020+
  1371. level5n:
  1372.      movem.l  d0-d1/a0-a1/a5-a6,-(sp)          ; save registers
  1373.      lea      _custom,a0                  ; get ptr to custom regs
  1374.      move.w   intenar(a0),d1              ; get enabled interrupts
  1375.      and.w      intreqr(a0),d1              ; and in requested interrupts
  1376.      movea.l  SysBase(pc),a6              ; get ExecBase
  1377.      btst      #INTB_DSKSYNC,d1              ; Disk synchronized?
  1378.      beq.b      10$                      ; nope, branch
  1379.      movem.l  IVDSKSYNC(A6),a1/a5              ; get data and code ptrs
  1380.      jsr      (a5)                      ; branch to routine
  1381. 10$     movem.l  (sp)+,d0-d1/a0-a1/a5-a6          ; restore registers
  1382.      rte                          ; return
  1383.      ;
  1384.      ;      Default Level 5 handler
  1385.      ;
  1386.      ;  a1 - is_Data
  1387.      ;
  1388.      cnop 0,4 ; align for 020+
  1389. level5:
  1390.      btst      #INTB_INTEN-8,_intenar          ; interrupts enabled?
  1391.      beq.b      45$                      ; nope, ignore
  1392.      btst      #INTB_RBF-8,_intreqr              ; receive buffer full?
  1393.      beq.b      level5n                  ; nope, invoke old handler
  1394.      ;
  1395.      move.l   a0,-(sp)                  ; save registers
  1396.      move.l   a1,-(sp)                  ; save registers
  1397.      lea      _custom,a1                  ; get ptr to custom regs
  1398.      ;
  1399. 10$     btst      #7,serdatr(a1)              ; Overrun?
  1400.      bne.b      50$                      ; yep, branch
  1401.      ;
  1402. 20$     movea.l  i_BufIn(pc),a0              ; get current ptr
  1403.      move.b   serdatr+1(a1),(a0)+              ; store received byte
  1404.      move.w   #INTF_RBF,intreq(a1)              ; clear RBF interrupt
  1405.      addq.l   #1,i_InCnt                  ; incr bytes in buffer
  1406.      ;
  1407.      cmpa.l   i_BufEnd(pc),a0              ; hit end of buffer?
  1408.      beq.b      60$                      ; yep, branch
  1409. 30$     move.l   a0,i_BufIn                  ; store input ptr
  1410.      ;
  1411.      subq.l   #1,i_Thresh                  ; close to full buffer?
  1412.      beq.b      70$                      ; yep, branch
  1413.      ;
  1414. 40$     btst      #INTB_RBF-8,intreqr(a1)          ; receive buffer full?
  1415.      bne.b      10$                      ; yep, go get another byte
  1416.      ;
  1417.      movea.l  (sp)+,a1                  ; restore registers
  1418.      movea.l  (sp)+,a0                  ; restore registers
  1419. 45$     rte                          ; return
  1420.      ;
  1421.      ;      We've missed some data, so set overrun flag.
  1422.      ;
  1423. 50$     addq.b   #1,Overrun                  ; set overrun flag
  1424.      bra.b      20$                      ; continue
  1425.      ;
  1426.      ;      Hit physical end of buffer, so wrap to the start of the buffer.
  1427.      ;
  1428.      cnop 0,4 ; align for 020+
  1429. 60$     movea.l  i_BufPtr(pc),a0              ; get buffer ptr
  1430.      bra.b      30$                      ; continue
  1431.      ;
  1432.      ;      Hit buffer threshold, so tell other end not to send any more
  1433.      ;      data.
  1434.      ;
  1435.      cnop 0,4 ; align for 020+
  1436. 70$     tst.b      Handshake(pc)               ; are we handshaking?
  1437.      beq.b      40$                      ; nope, skip RTS
  1438.      ori.b      #1<<CIAB_COMRTS,_ciabpra          ; block further input
  1439.      bra.b      40$                      ; go return
  1440.      ;
  1441.      ;
  1442.      ;  a1 - IS_DATA
  1443.      ;  a5 - jump vector register
  1444.      ;
  1445.      cnop 0,4 ; align for 020+
  1446. level2:
  1447.      ;
  1448.      ;      If there's nothing in the buffer, there's no point in going
  1449.      ;      any further.
  1450.      ;
  1451.      tst.l      i_InCnt(pc)                  ; anything in the buffer?
  1452.      bne.b      20$                      ; yep, branch
  1453. 10$     moveq      #0,d0                   ; set Z flag
  1454.      rts                          ; return
  1455.      ;
  1456.      ;      If we've been "disabled" then get out.
  1457.      ;
  1458. 20$     tst.b      disableRead(pc)              ; internally disabled?
  1459.      bge.b      10$                      ; yep, get out of here
  1460.      lea.l      Start(pc),a5                  ; get base
  1461.      ;
  1462.      ;      If we have an active request, branch down and try to fulfill it.
  1463.      ;
  1464.      move.l   cr_IOReq(pc),d0              ; get and test active I/O
  1465.      bne.b      30$                      ; nzero, active, branch
  1466.      ;
  1467.      ;      Get first node in list and test if empty.
  1468.      ;
  1469.      move.l   (a1),a0                  ; get head of list
  1470.      move.l   (a0),d0                  ; get successor
  1471.      beq.b      10$                      ; end of list? yep, branch
  1472.      ;
  1473.      ;      Remove the node from the list
  1474.      ;
  1475.      move.l   d0,(a1)                  ; make new head
  1476.      exg.l      d0,a0                   ; swap nodes
  1477.      move.l   a1,LN_PRED(a0)              ; store predecessor
  1478.      ;
  1479.      ;      Setup fields for processing a read request
  1480.      ;
  1481.      movea.l  d0,a1                   ; get I/O request
  1482.      move.b   IO_FLAGS(a1),d1
  1483.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1484.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1485.      move.b   d1,IO_FLAGS(a1)
  1486.      move.l   a1,cr_IOReq-Start(a5)           ; store I/O request
  1487.      move.l   IO_DATA(a1),cr_OutPtr-Start(a5)     ; get/set output ptr
  1488.      move.l   IO_LENGTH(a1),cr_Length-Start(a5)   ; get/set output count
  1489.      ;
  1490.      ;      Process an active I/O request
  1491.      ;
  1492. 30$     movea.l  SysBase(pc),a6              ; get ExecBase
  1493.      move.l   d0,a1                   ; get I/O request
  1494.      bsr.b      copyData                  ; go copy 'em
  1495.      tst.l      d0                      ; done with request?
  1496.      beq.b      10$                      ; nope, branch
  1497.      ;
  1498.      ;      the request has been satisfied, so return it.
  1499.      ;
  1500.      clr.l      cr_IOReq-Start(a5)              ; clear request
  1501.      andi.b   #~(1<<IOSERB_ACTIVE),IO_FLAGS(a1)   ; no longer active
  1502.      jsr      _LVOReplyMsg(a6)              ; return I/O
  1503.      bra.b      10$
  1504.      ;
  1505.      ;      Registers:
  1506.      ;      Entry:   A1        Ptr to I/O Request
  1507.      ;           A6        ExecBase
  1508.      ;      Exit:    D0        ZERO - request not done, do not reply it
  1509.      ;                NZERO - request done, reply it
  1510.      ;
  1511.      cnop 0,4 ; align for 020+
  1512. copyData:
  1513.      ;
  1514.      ;      Process an active I/O request (or fall through from above)
  1515.      ;
  1516.      movem.l  d2-d5/a1-a5,-(sp)              ; save registers
  1517.      lea.l      Start(pc),a5                  ; get base
  1518.      movea.l  a1,a4                   ; get I/O request
  1519.      ;
  1520.      movea.l  i_BufOut(pc),a2              ; get current bufout ptr
  1521.      movea.l  cr_OutPtr(pc),a3              ; get current output ptr
  1522.      move.l   cr_Length(pc),d2              ; get current bytes needed
  1523.      move.l   i_InCnt(pc),d3              ; get current bytes in buffer
  1524.      ;
  1525.      ;      If we don't have enough bytes to satisfy the request,
  1526.      ;      set the length to the number of bytes we do have.
  1527.      ;
  1528.      cmp.l      d2,d3                   ; enuf to satisfy request?
  1529.      bge.b      15$                      ; yep, so branch
  1530.      move.l   d3,d2                   ; # to copy = # in buffer
  1531.      bra.b      15$                      ; branch to loop entry
  1532.      ;
  1533.      ;      Start of copy loop.
  1534.      ;
  1535.      cnop 0,4 ; align for 020+
  1536. 10$     movea.l  i_BufPtr(pc),a2              ; reset bufout to start
  1537.      ;
  1538.      ;      Entry point of copy loop.
  1539.      ;
  1540. 15$     move.l   d2,d3                   ; xfer # of bytes to copy
  1541.      ;
  1542.      ;      If the copy will extend past the end of the buffer, we can
  1543.      ;      only copy the number of bytes to the end this go around.
  1544.      ;
  1545.      move.l   i_BufEnd(pc),d0              ; get ptr to end of buf
  1546.      sub.l      a2,d0                   ; calc # of bytes to end
  1547.      cmp.l      d0,d3                   ; # to copy < # to end?
  1548.      blt.b      20$                      ; yep, branch
  1549.      move.l   d0,d3                   ; get bytes to end
  1550.      ;
  1551.      ;      Registers:
  1552.      ;
  1553.      ;      A2 = pointer from which data will be copied
  1554.      ;      A3 = pointer to which data will be copied
  1555.      ;      D2 = number of bytes that need to be copied
  1556.      ;      D3 = number of bytes to copy this iteration
  1557.      ;
  1558.     IFNE EOFCODE
  1559. 20$     btst      #SERB_EOFMODE,IO_SERFLAGS(a4)       ; EOFMODE requested?
  1560.      beq.b      30$                      ; nope, just go copy
  1561.      ;
  1562.      ;      EOFMODE was specified so copy characters 1 at a time until
  1563.      ;      we hit an EOF character, the output butter has filled, or
  1564.      ;      the input buffer has drained.
  1565.      ;
  1566.      move.l   d3,d0                   ; get length
  1567.      ;
  1568.      lea.l      IO_TERMARRAY(a4),a1              ; get termarry ptr
  1569. 21$     move.b   (a2)+,d1                  ; get byte
  1570.      ; This way the term char IS copied (if term char is not copied eofmode will fail)
  1571.      move.b   d1,(a3)+                  ; put in output buffer
  1572.      ;
  1573.      cmp.b      (a1),d1                  ; found term char?
  1574.      bge.b      22$                      ; possibly, branch
  1575.      cmp.b      1(a1),d1                  ; found term char?
  1576.      bge.b      22$                      ; possibly, branch
  1577.      cmp.b      2(a1),d1                  ; found term char?
  1578.      bge.b      22$                      ; possibly, branch
  1579.      cmp.b      3(a1),d1                  ; found term char?
  1580.      bge.b      22$                      ; possibly, branch
  1581.      cmp.b      4(a1),d1                  ; found term char?
  1582.      bge.b      22$                      ; possibly, branch
  1583.      cmp.b      5(a1),d1                  ; found term char?
  1584.      bge.b      22$                      ; possibly, branch
  1585.      cmp.b      6(a1),d1                  ; found term char?
  1586.      bge.b      22$                      ; possibly, branch
  1587.      cmp.b      7(a1),d1                  ; found term char?
  1588.      bgt.b      23$                      ; nope, branch
  1589. 22$     beq.b      24$                      ; term char found?
  1590.      ;
  1591.      ;      Didn't find a term character, so continue with the copy loop
  1592.      ;
  1593. 23$
  1594.      subq.l   #1,d0                   ; decr length counter
  1595.      bne.b      21$                      ; continue if more
  1596.      bra      40$                      ; done with copy, branch
  1597.      ;
  1598.      ;      We've found a termination character.
  1599.      ;
  1600.      cnop 0,4 ; align for 020+
  1601. 24$     clr.l      cr_Length-Start(a5)              ; done with request
  1602.      bra      50$                      ; branch
  1603.      cnop 0,4 ; align for 020+
  1604.     ELSE
  1605. 20$
  1606.     ENDC
  1607.      ;
  1608.      ;      EOFMODE not specified, so just do a bulk copy.
  1609.      ;
  1610.      ;      XXX POSSIBLE SPEEDUP XXX
  1611.      ;
  1612.      ;      For short copies it would be quicker to have a simple inline
  1613.      ;      loop, but what's short???  It would be different by CPU.
  1614.      ;
  1615. 30$
  1616.      move.l   d3,d0                   ; get length
  1617.      movea.l  a3,a1                   ; where to put it
  1618.      movea.l  a2,a0                   ; where to get it
  1619.  
  1620.      movem.l  d2-d7/a2-a6,-(sp)
  1621. ; we assume buffers are aligned
  1622. ; NB: buffer size is multiple of 64bytes (not contents size)
  1623. ; Copy should fit on 256 Kb Inst Cache.
  1624. ; Copy loop for 1K blocks
  1625. 35$     cmp.l      #1024,d0
  1626.      blo      100$
  1627. ; Copy 1008 bytes
  1628.      movem.l  (a0)+,d1-d7/a2-a6
  1629.      movem.l  d1-d7/a2-a6,(a1)
  1630.      movem.l  (a0)+,d1-d7/a2-a6
  1631.      movem.l  d1-d7/a2-a6,48(a1)
  1632.      movem.l  (a0)+,d1-d7/a2-a6
  1633.      movem.l  d1-d7/a2-a6,96(a1)
  1634.      movem.l  (a0)+,d1-d7/a2-a6
  1635.      movem.l  d1-d7/a2-a6,144(a1)
  1636.      movem.l  (a0)+,d1-d7/a2-a6
  1637.      movem.l  d1-d7/a2-a6,192(a1)
  1638.      movem.l  (a0)+,d1-d7/a2-a6
  1639.      movem.l  d1-d7/a2-a6,240(a1)
  1640.      movem.l  (a0)+,d1-d7/a2-a6
  1641.      movem.l  d1-d7/a2-a6,288(a1)
  1642.      movem.l  (a0)+,d1-d7/a2-a6
  1643.      movem.l  d1-d7/a2-a6,336(a1)
  1644.      movem.l  (a0)+,d1-d7/a2-a6
  1645.      movem.l  d1-d7/a2-a6,384(a1)
  1646.      movem.l  (a0)+,d1-d7/a2-a6
  1647.      movem.l  d1-d7/a2-a6,432(a1)
  1648.      movem.l  (a0)+,d1-d7/a2-a6
  1649.      movem.l  d1-d7/a2-a6,480(a1)
  1650.      movem.l  (a0)+,d1-d7/a2-a6
  1651.      movem.l  d1-d7/a2-a6,528(a1)
  1652.      movem.l  (a0)+,d1-d7/a2-a6
  1653.      movem.l  d1-d7/a2-a6,576(a1)
  1654.      movem.l  (a0)+,d1-d7/a2-a6
  1655.      movem.l  d1-d7/a2-a6,624(a1)
  1656.      movem.l  (a0)+,d1-d7/a2-a6
  1657.      movem.l  d1-d7/a2-a6,672(a1)
  1658.      movem.l  (a0)+,d1-d7/a2-a6
  1659.      movem.l  d1-d7/a2-a6,720(a1)
  1660.      movem.l  (a0)+,d1-d7/a2-a6
  1661.      movem.l  d1-d7/a2-a6,768(a1)
  1662.      movem.l  (a0)+,d1-d7/a2-a6
  1663.      movem.l  d1-d7/a2-a6,816(a1)
  1664.      movem.l  (a0)+,d1-d7/a2-a6
  1665.      movem.l  d1-d7/a2-a6,864(a1)
  1666.      movem.l  (a0)+,d1-d7/a2-a6
  1667.      movem.l  d1-d7/a2-a6,912(a1)
  1668.      movem.l  (a0)+,d1-d7/a2-a6
  1669.      movem.l  d1-d7/a2-a6,960(a1)
  1670.      lea.l      1008(a1),a1
  1671. ; Copy 16 bytes
  1672.      move16   (a0)+,(a1)+
  1673.      sub.l      #1024,d0
  1674.      bne      35$
  1675.      bra.b      105$
  1676.      cnop 0,4 ; align for 020+
  1677. 100$     move.l  SysBase(pc),a6
  1678.      jsr     _LVOCopyMem(a6)
  1679. 105$     movem.l (sp)+,d2-d7/a2-a6
  1680.      adda.l   d3,a2                   ; update bufout
  1681.      adda.l   d3,a3                   ; update outptr
  1682.      ;
  1683.      ;      Fall through and entered from EOFMODE loop
  1684.      ;
  1685.      ;      If the following calculation results in a value greater than
  1686.      ;      zero, then we have a buffer wrap and need to process the
  1687.      ;      remaining bytes at the beginning of the buffer.
  1688.      ;
  1689. 40$     sub.l      d3,d2                   ; calc bytes left to copy
  1690.      bgt      10$                      ; >0, more to copy, branch
  1691.      ;
  1692.      ;
  1693.      ;
  1694. 50$     move.l   a3,d1                   ; get outptr
  1695.      sub.l      cr_OutPtr(pc),d1              ; calc length
  1696.      move.l   a3,cr_OutPtr-Start(a5)          ; update outptr
  1697.      ;
  1698.      ;      Update output ptr and I/O request
  1699.      ;
  1700.      move.l   a2,i_BufOut-Start(a5)           ; store bufout ptr
  1701.      add.l      d1,IO_ACTUAL(a4)              ; update I/O request
  1702.      ;
  1703.      ;      Update number of bytes left in the buffer.
  1704.      ;
  1705.      sub.l      d1,i_InCnt-Start(a5)              ; calc bytes left in buffer
  1706.      ;
  1707.      ;      If the threshold becomes positive here, then, if requested,
  1708.      ;      tell the other end that it's okay to start sending more data.
  1709.      ;
  1710.      add.l      d1,i_Thresh-Start(a5)           ; calc thresh and test
  1711.      ble.b      60$                      ; <= 0, need more data, skip
  1712.      tst.b      Handshake(pc)               ; are we handshaking?
  1713.      beq.b      60$                      ; nope, skip RTS
  1714.      andi.b   #~(1<<CIAB_COMRTS),_ciabpra          ; ready to recieve more data
  1715.      ;
  1716.      ;      Set error if we've had any overruns.
  1717.      ;
  1718. 60$     tst.b      Overrun(pc)                  ; did overrun occur?
  1719.      beq.b      70$                      ; nope, branch
  1720.      clr.b      Overrun-Start(a5)              ; reset overrun flag
  1721.      move.b   #SerErr_LineErr,IO_ERROR(a4)          ; set error code
  1722.      ;
  1723.      ;      Calc number of bytes left to copy.  If the result is greater
  1724.      ;      than zero, then we have more to copy so return an incomplete
  1725.      ;      status.
  1726.      ;
  1727. 70$     moveq      #0,d0                   ; assume I/O incomplete
  1728.      sub.l      d1,cr_Length-Start(a5)          ; update length and test
  1729.      bgt.b      90$                      ; >0, more to do, branch
  1730.      ;
  1731.      ;      We've completed the I/O request either by copying the requested
  1732.      ;      of bytes or by finding an EOFMODE character, so return a "reply"
  1733.      ;      status.
  1734.      ;
  1735.      moveq      #1,d0                   ; indicate reply
  1736.      ;
  1737.      ;      Restore and exit
  1738.      ;
  1739. 90$     movem.l  (sp)+,d2-d5/a1-a5              ; restore registers
  1740.      rts                          ; return (status in D0)
  1741.      ;
  1742.      ;      Align data
  1743.      ;
  1744.      CNOP      0,4
  1745.      ;
  1746.      ;
  1747.      ;
  1748. Init:
  1749.      DC.L      sizeof_Base8n1
  1750.      DC.L      funcTab
  1751.      DC.L      dataTab
  1752.      DC.L      InitRoutine
  1753.      ;
  1754.      ;
  1755.      ;
  1756. funcTab:
  1757.      DC.W      -1
  1758.      DC.W      dev_Open-funcTab
  1759.      DC.W      dev_Close-funcTab
  1760.      DC.W      dev_Expunge-funcTab
  1761.      DC.W      dev_Null-funcTab
  1762.      DC.W      dev_BeginIO-funcTab
  1763.      DC.W      dev_AbortIO-funcTab
  1764.      DC.W      -1
  1765.      ;
  1766.      ;
  1767.      ;
  1768. dataTab:
  1769.      INITBYTE LN_TYPE,NT_DEVICE
  1770.      INITLONG LN_NAME,Name
  1771.      INITBYTE LIB_FLAGS,LIBF_SUMUSED|LIBF_CHANGED
  1772.      INITWORD LIB_VERSION,VERSION
  1773.      INITWORD LIB_REVISION,REVISION
  1774.      INITLONG LIB_IDSTRING,IdString
  1775.      DC.W      0
  1776.      ;
  1777.      ;      String Constants
  1778.      ;
  1779. miscresource:
  1780.      DC.B      "misc.resource",0
  1781. timerdevice:
  1782.      DC.B      "timer.device",0
  1783. intuitlib:
  1784.      DC.B      "intuition.library",0
  1785. Name:
  1786.      DC.B      "8n1.device",0
  1787. IdString:
  1788.      VSTRING
  1789.      ;
  1790.      ;      End of checksummed area.  (Realigns data too!)
  1791.      ;
  1792. ENDTag:
  1793.      CNOP      0,4
  1794.      ;
  1795.      ;      Global SysBase (Use instead of AbsExecBase for speed)
  1796.      ;
  1797. SysBase:
  1798.      DC.L      0
  1799.      ;
  1800.      ;      Internal buffer tracking (DO NOT CHANGE THE ORDER!!!!)
  1801.      ;
  1802. i_BufPtr:
  1803.      DC.L      0
  1804. i_BufIn:
  1805.      DC.L      0
  1806. i_BufOut:
  1807.      DC.L      0
  1808. i_BufEnd:
  1809.      DC.L      0
  1810. i_InCnt:
  1811.      DC.L      0
  1812. i_Thresh:
  1813.      DC.L      0
  1814.      ;
  1815.      ;      Used while processing a read request.
  1816.      ;
  1817. cr_IOReq:
  1818.      DC.L      0
  1819. cr_OutPtr:
  1820.      DC.L      0
  1821. cr_Length:
  1822.      DC.L      0
  1823.      ;
  1824.      ;      List head for read requests
  1825.      ;
  1826. readQ:
  1827.      DC.L      readQ+MLH_TAIL
  1828.      DC.L      0
  1829.      DC.L      readQ
  1830.      ;
  1831.      ;      Write control.
  1832.      ;
  1833. cw_Length:
  1834.      DC.L      0
  1835. cw_Buffer:
  1836.      DC.L      0
  1837. cw_IOReq:
  1838.      DC.L      0
  1839.      ;
  1840.      ;      List head for write requests
  1841.      ;
  1842. writeQ:
  1843.      DC.L      writeQ+MLH_TAIL
  1844.      DC.L      0
  1845.      DC.L      writeQ
  1846.      ;
  1847.      ;
  1848.      ;
  1849. timerPort:
  1850.      DC.L      0                      ; LN_SUCC
  1851.      DC.L      0                      ; LN_PRED
  1852.      DC.B      NT_MSGPORT                  ; LN_TYPE
  1853.      DC.B      0                      ; LN_PRI
  1854.      DC.L      0                      ; LN_NAME
  1855.      DC.B      3                      ; MP_FLAGS (undoc'ed)
  1856.      DC.B      0                      ; MP_SIGBIT
  1857.      DC.L      timerRtn                  ; MP_SIGTASK
  1858.      DC.L      timerPort+MP_MSGLIST+LH_TAIL          ; LH_HEAD
  1859.      DC.L      0                      ; LH_TAIL
  1860.      DC.L      timerPort+MP_MSGLIST              ; LH_TAILPRED
  1861.      DC.B      0                      ; LH_TYPE
  1862.      DC.B      0                      ; LH_pad
  1863.      DC.W      0                      ; long align
  1864.      ;
  1865.      ;
  1866.      ;
  1867. timerReq:
  1868.      DC.L      0                      ; LN_SUCC
  1869.      DC.L      0                      ; LN_PRED
  1870.      DC.B      NT_MESSAGE                  ; LN_TYPE
  1871.      DC.B      0                      ; LN_PRI
  1872.      DC.L      0                      ; LN_NAME
  1873.      DC.L      timerPort                  ; MN_REPLYPORT
  1874.      DC.W      IOTV_SIZE                  ; MN_LENGTH
  1875.      DC.L      0                      ; IO_DEVICE
  1876.      DC.L      0                      ; IO_UNIT
  1877.      DC.W      TR_ADDREQUEST               ; IO_COMMAND
  1878.      DC.B      0                      ; IO_FLAGS
  1879.      DC.B      0                      ; IO_ERROR
  1880.      DC.L      0                      ; TV_SECS
  1881.      DC.L      0                      ; TV_MICROS
  1882.      ;
  1883.      ;
  1884.      ;
  1885. VBInterrupt:
  1886.      DC.L      0                      ; LN_SUCC
  1887.      DC.L      0                      ; LN_PRED
  1888.      DC.B      NT_INTERRUPT                  ; LN_TYPE
  1889.      DC.B      0                      ; LN_PRI
  1890.      DC.L      Name                      ; LN_NAME
  1891.      DC.L      readQ                   ; IS_DATA
  1892.      DC.L      level2                  ; IS_CODE
  1893.      ;
  1894.      ;      Global flags
  1895.      ;
  1896. Overrun:
  1897.      DC.B      0
  1898. Handshake:
  1899.      DC.B      1
  1900. disableRead:
  1901.      DC.B      -1
  1902.      ;
  1903.      ;
  1904.      ;
  1905.      END
  1906.  
  1907.